package cn.edu.bs.service.impl;

import cn.edu.bs.dao.mapper.BookMapper;
import cn.edu.bs.domain.Book;
import cn.edu.bs.domain.BookExample;
import cn.edu.bs.domain.custom.BookCustom;
import cn.edu.bs.service.BookService;
import cn.edu.bs.service.TypeService;
import cn.edu.bs.web.request.book.BookFormCommand;
import cn.edu.bs.web.vo.BookVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author bs
 */
@Service("bookService")
public class BookServiceImpl implements BookService {

    private final BookMapper bookMapper;

    private final TypeService typeService;

    @Autowired
    public BookServiceImpl(BookMapper bookMapper, TypeService typeService) {
        this.bookMapper = bookMapper;
        this.typeService = typeService;
    }

    @Override
    public long countByExample(BookExample example) {
        return bookMapper.countByExample(example);
    }

    @Override
    public int deleteByExample(BookExample example) {
        return bookMapper.deleteByExample(example);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return bookMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Book record) {
        return bookMapper.insert(record);
    }

    @Override
    public int insertSelective(Book record) {
        return bookMapper.insertSelective(record);
    }

    @Override
    public List<Book> selectByExample(BookExample example) {
        return bookMapper.selectByExample(example);
    }

    @Override
    public Book selectByPrimaryKey(Integer id) {
        return bookMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByExampleSelective(Book record, BookExample example) {
        return bookMapper.updateByExampleSelective(record, example);
    }

    @Override
    public int updateByExample(Book record, BookExample example) {
        return bookMapper.updateByExample(record, example);
    }

    @Override
    public int updateByPrimaryKeySelective(Book record) {
        return bookMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Book record) {
        return bookMapper.updateByPrimaryKey(record);
    }

    @Override
    public Book transformCommandToEntity(BookFormCommand command) {
        Book book = new Book();
        BeanUtils.copyProperties(command, book);
        return book;
    }

    @Override
    public BookVO transformEntityToVO(Book entity) {
        BookVO vo = new BookVO();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }

    @Override
    public BookVO transformCustomToVO(BookCustom custom) {
        BookVO vo = new BookVO();
        BeanUtils.copyProperties(custom, vo);
        if (custom.getTypeCustom() != null) {
            vo.setTypeVO(typeService.transformCustomToVO(custom.getTypeCustom()));
        }
        return vo;
    }

    @Override
    public List<BookVO> transformEntitiesToTOs(List<Book> entities) {
        List<BookVO> vos = new ArrayList<>();
        for (Book book : entities) {
            vos.add(transformEntityToVO(book));
        }
        return vos;
    }

    @Override
    public List<BookVO> transformCustomsToTOs(List<BookCustom> customs) {
        List<BookVO> vos = new ArrayList<>();
        for (Book book : customs) {
            vos.add(transformEntityToVO(book));
        }
        return vos;
    }
}
